<?php
namespace app\admin\controller;
use app\admin\logic\UserLogic;

/**
 * 用户管理api控制器
 * Created by PhpStorm.
 * User: tyll
 * Date: 2019/09/17
 * Time: 09:26
 */
class User extends Base {

    public function __construct(){
        parent::__construct();
        $this->u_logic = new UserLogic();
    }

    /**
     * 获取用户
     * @return \think\response\Json
     */
    public function ajax_get_user(){
        $params = input();
        $r = $this->check_admin_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        $maps = $this->u_logic->get_search_user_maps($params['username'],$params['department'],$params['position'],$params['status']);
        $limit = ($params['page'] - 1) * $params['rows'];

        $list = model('user')->get_list($maps,'id,role,username,tel,department,position,status','id desc','',$limit,$params['rows']);
        foreach($list as $k => $v){
            $r_maps = [];
            $r_maps[] = ['id','=',$v['role']];
            $r_detail = model('role')->get_detail($r_maps,'id,name');
            $list[$k]['role_zw'] = !empty($r_detail) ? $r_detail['name'] : '';
        }

        $total_num = model('user')->get_count($maps,'id');

        $data['list'] = $list;
        $data['total_num'] = $total_num;
        return json($this->u_logic->get_response(1,'OK',$data));
    }

    /**
     * 获取用户详情
     * @return \think\response\Json
     */
    public function ajax_get_user_detail(){
        $params = input();
        $r = $this->check_admin_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        $maps = [];
        $maps[] = ['id','=',$params['userid']];
        $u_detail =  model('user')->get_detail($maps,'id,openid,role,username,tel,department,position');

        $maps = [];
        $maps[] = ['openid','=',$u_detail['openid']];
        $up_detail =  model('user_power')->get_detail($maps,'id,admin_power,user_power');
        $u_detail['admin_power'] = !empty($up_detail) ? json_decode($up_detail['admin_power'],true) : [];
        $u_detail['user_power'] = !empty($up_detail) ? json_decode($up_detail['user_power'],true) : [];

        $data['u_detail'] = $u_detail;
        return json($this->u_logic->get_response(1,'OK',$data));
    }

    /**
     * 修改用户
     * @return \think\response\Json
     */
    public function ajax_save_user(){
        $params = input();
        $r = $this->check_admin_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        //检查后台用户权限规则
        $r = $this->u_logic->check_admin_rule();
        if(!$r['code']){
            return json($r);
        }
        $a_detail = $r['data']['detail'];

        //检查后台用户权限规则
        $r = $this->u_logic->check_admin_role_rule();
        if(!$r['code']){
            return json($r);
        }

        //检查修改用户规则
        $r = $this->u_logic->check_save_user_rule($a_detail['role'],$params);
        if(!$r['code']){
            return json($r);
        }
        $u_detail = $r['data']['u_detail'];

        $u_data = [];
        $u_data['username'] = trim($params['username']);
        $u_data['department'] = trim($params['department']);
        $u_data['position'] = trim($params['position']);
        $u_data['tel'] = trim($params['tel']);
        $u_data['role'] = $params['role'];

        $maps = [];
        $maps[] = ['id','=',$u_detail['id']];
        $r = model('user')->force()->save($u_data,$maps);
        if($r !== false){
            //修改用户权限
            $r = $this->u_logic->save_user_power($u_detail['openid'],$params);
            return json($this->u_logic->get_response(1,'修改成功'));
        }
        return json($this->u_logic->get_response(0,'修改失败'));
    }

    /**
     * 审核通过
     * @return \think\response\Json
     */
    public function ajax_examine_pass(){
        $params = input();
        $r = $this->check_admin_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        //检查后台用户权限规则
        $r = $this->u_logic->check_admin_rule();
        if(!$r['code']){
            return json($r);
        }
        $a_detail = $r['data']['detail'];

        //检查后台用户权限规则
        $r = $this->u_logic->check_admin_role_rule();
        if(!$r['code']){
            return json($r);
        }

        //检查审核规则
        $r = $this->u_logic->check_examine_rule($a_detail['role'],$params);
        if(!$r['code']){
            return json($r);
        }
        $u_detail = $r['data']['u_detail'];

        //更新审核状态
        $u_data = [];
        $u_data['status'] = 1;

        $maps = [];
        $maps[] = ['id','=',$u_detail['id']];
        $r = model('user')->force()->save($u_data,$maps);
        if($r !== false){
            return json($this->u_logic->get_response(1,'操作成功'));
        }
        return json($this->u_logic->get_response(0,'操作失败'));
    }

    /**
     * 审核驳回
     * @return \think\response\Json
     */
    public function ajax_examine_reject(){
        $params = input();
        $r = $this->check_admin_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        //检查后台用户权限规则
        $r = $this->u_logic->check_admin_rule();
        if(!$r['code']){
            return json($r);
        }
        $a_detail = $r['data']['detail'];

        //检查后台用户权限规则
        $r = $this->u_logic->check_admin_role_rule();
        if(!$r['code']){
            return json($r);
        }

        //检查审核规则
        $r = $this->u_logic->check_examine_rule($a_detail['role'],$params);
        if(!$r['code']){
            return json($r);
        }
        $u_detail = $r['data']['u_detail'];

        //更新审核状态
        $u_data = [];
        $u_data['status'] = 2;

        $maps = [];
        $maps[] = ['id','=',$u_detail['id']];
        $r = model('user')->force()->save($u_data,$maps);
        if($r !== false){
            return json($this->u_logic->get_response(1,'操作成功'));
        }
        return json($this->u_logic->get_response(0,'操作失败'));
    }

    /**
     * 删除用户
     * @return \think\response\Json
     */
    public function ajax_del_user(){
        $params = input();
        $r = $this->check_admin_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        //检查后台用户权限规则
        $r = $this->u_logic->check_admin_rule();
        if(!$r['code']){
            return json($r);
        }
        $a_detail = $r['data']['detail'];

        //检查后台用户权限规则
        $r = $this->u_logic->check_admin_role_rule();
        if(!$r['code']){
            return json($r);
        }

        //检查审核规则
        $r = $this->u_logic->check_del_user_rule($a_detail['role'],$params);
        if(!$r['code']){
            return json($r);
        }
        $u_detail = $r['data']['u_detail'];

        $r = model('user')->destroy($u_detail['id']);
        if($r){
            return json($this->u_logic->get_response(1,'删除成功'));
        }
        return json($this->u_logic->get_response(0,'删除失败'));
    }
}